<script setup>
import { User, Lock, Right } from '@element-plus/icons-vue'
import { userRegisterService, userLoginService } from '@/api/user'
import { useUserStore } from '@/stores'
import { useRouter } from 'vue-router'
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { watch } from 'vue'
const formRef = ref()
const checked1 = ref(false)
const isLogin = ref(true)
const formModel = ref({
    username: '',
    password: '',
    repassword: ''
})
const regRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, max: 10, message: '用户必须是5-10位的字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符',
            trigger: 'blur'
        }
    ],
    repassword: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
            pattern: /^\S{6,15}$/,
            message: '密码必须是6-15位的非空字符',
            trigger: 'blur'
        },
        {  // 自定义校验
            //  validator: (rule, value, callback)
            // rule  当前校验规则相关的信息
            // value 所校验的表单元素目前的表单值
            // callback 无论成功还是失败，都需要callback回调
            //      -callback() 校验成功
            //      -callback(new Error(错误信息)) 校验失败
            validator: (rule, value, callback) => {
                if (value !== formModel.value.password) {
                    callback(new Error('两次密码输入不一致'))
                } else {
                    callback()
                }
            },
            trigger: 'blur'
        }
    ]
}
const regBtn = async () => {
    await formRef.value.validate()
    // console.log("全部校验成功，开始注册请求");
    await userRegisterService(formModel.value)
    ElMessage.success('注册成功')
    isLogin.value = true
}
const userStore = useUserStore()
const router = useRouter()
const loginBtn = async () => {
    await formRef.value.validate()
    // console.log("全部校验成功，开始登录请求");
    const res = await userLoginService(formModel.value)
    userStore.setToken(res.data.token)
    ElMessage.success('登录成功')
    router.push('/')


}
// 监听 如果进行切换  重置表单
watch(isLogin, () => {
    formModel.value = {
        username: '',
        password: '',
        repassword: ''
    }
})
</script>

<template>
    <el-row class="login-page">
        <!-- 左侧背景区域 -->
        <el-col :span="12" class="bg">
            <div class="bg-content">
                <h2>欢迎来到黑马前端</h2>
                <p>专业的前端学习平台</p>
            </div>
        </el-col>

        <!-- 右侧表单区域 -->
        <el-col :span="6" :offset="3" class="colBox">
            <div class="form-container">
                <!-- 登录表单 -->
                <el-form class="auth-form" ref="formRef" :model="formModel" :rules="regRules" v-if="isLogin">
                    <el-form-item>
                        <h1>登录</h1>
                    </el-form-item>

                    <el-form-item prop="username">
                        <el-input placeholder="请输入用户名" :prefix-icon="User" size="large"
                            v-model="formModel.username"></el-input>
                    </el-form-item>

                    <el-form-item prop="password">
                        <el-input placeholder="请输入密码" :prefix-icon="Lock" type="password" size="large" show-password
                            v-model="formModel.password"></el-input>
                    </el-form-item>

                    <el-form-item class="action-row">
                        <div class="remember-forgot">
                            <el-checkbox v-model="checked1" label="记住我" size="large" />
                            <el-link type="primary" :underline="false">忘记密码？</el-link>
                        </div>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" size="large" class="auth-btn" @click="loginBtn">登录</el-button>
                    </el-form-item>

                    <el-form-item class="switch-link">
                        <span>还没有账号？</span>
                        <el-link :underline="false" @click="isLogin = false" type="primary">
                            立即注册
                            <el-icon>
                                <Right />
                            </el-icon>
                        </el-link>
                    </el-form-item>
                </el-form>

                <!-- 注册表单 -->
                <el-form class="auth-form" ref="formRef" :model="formModel" :rules="regRules" v-else>
                    <el-form-item>
                        <h1>注册</h1>
                    </el-form-item>

                    <el-form-item prop="username">
                        <el-input placeholder="请输入用户名" :prefix-icon="User" size="large"
                            v-model="formModel.username"></el-input>
                    </el-form-item>

                    <el-form-item prop="password">
                        <el-input placeholder="请输入密码" :prefix-icon="Lock" type="password" size="large" show-password
                            v-model="formModel.password"></el-input>
                    </el-form-item>

                    <el-form-item prop="repassword">
                        <el-input placeholder="请确认密码" :prefix-icon="Lock" type="password" size="large" show-password
                            v-model="formModel.repassword"></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button type="primary" size="large" class="auth-btn" @click="regBtn">注册</el-button>
                    </el-form-item>

                    <el-form-item class="switch-link">
                        <span>已有账号？</span>
                        <el-link :underline="false" @click="isLogin = true" type="primary">
                            立即登录
                            <el-icon>
                                <Right />
                            </el-icon>
                        </el-link>
                    </el-form-item>
                </el-form>
            </div>
        </el-col>
    </el-row>
</template>

<style scoped lang="scss">
.login-page {
    height: 100vh;
    margin: 0;

    .bg {
        background-color: rgba(141, 141, 237, 0.384);
        height: 100vh;
        border-radius: 0 15px 15px 0;
        display: flex;
        align-items: center;
        justify-content: center;

        .bg-content {
            color: white;
            text-align: center;

            h2 {
                font-size: 2rem;
                margin-bottom: 1rem;
            }

            p {
                font-size: 1.2rem;
            }
        }
    }

    .colBox {
        height: 100vh;
        display: flex;
        align-items: center;

        .form-container {
            width: 100%;
            min-height: 450px;
            /* 固定最小高度防止跳动 */
            display: flex;
            align-items: center;
        }

        .auth-form {
            width: 100%;
            transition: all 0.3s ease;

            h1 {
                text-align: center;
                margin-bottom: 30px;
                font-size: 24px;
            }

            .el-form-item {
                margin-bottom: 20px;

                :deep(.el-input__wrapper) {
                    border-radius: 6px;
                }
            }

            .action-row {
                .remember-forgot {
                    width: 100%;
                    display: flex;
                    justify-content: space-between;
                    align-items: center;
                }
            }

            .auth-btn {
                width: 100%;
                border-radius: 6px;
                margin-top: 10px;
            }

            .switch-link {
                display: flex;
                justify-content: center;
                align-items: center;
                margin-top: 15px;

                span {
                    margin-right: 8px;
                    color: var(--el-text-color-regular);
                }

                .el-link {
                    display: flex;
                    align-items: center;

                    .el-icon {
                        margin-left: 5px;
                    }
                }
            }
        }
    }
}
</style>